<template>
  <form-item-wrapper
    :designer="designer"
    :field="field"
    :rules="rules"
    :design-state="designState"
    :parent-widget="parentWidget"
    :parent-list="parentList"
    :index-of-parent-list="indexOfParentList"
    :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex"
    :sub-form-row-id="subFormRowId"
  >
  <!-- modify by caoy 2025/02/27 对象选择组件之前不支持隐藏值配置 现在增加 -->
  <template v-if="!field.options.hiddenField">
    <!-- modify by caoy 2025/03/20 去掉外层的div 避免超长时表单内的样式问题 -->
    <readonlyText
      v-if="isReadMode" :class="bindIsNormal ? 'read-normal-text' : ''"
      :list="readonlyTextList"
      labelField="widgetShowText"
      showOverflow
      isNeedOtypePermisson
      :isHasBackgroundBorder="bindIsNormal ? false : true"
      :isLink="field.options.isLink"
      :click="linkJump"
    />
    <div v-else class="select-Object-wrap">
      <div class="widget-wrap" ref="widgetWrapRef" @click="openDropdown" v-click-outside="closeDropdown">
        <select-object-show ref="fieldEditor" v-model="fieldModel" :field="field" :bindIsNormal="bindIsNormal" @change="handleChangeEvent" @remove="clearVal" />
        <div class="select-object-clear" v-if="isShowClear" :disabled="field.options.disabled" @click.stop="() => clearVal()">
          <el-icon class="clear">
            <CircleClose />
          </el-icon>
        </div>
      </div>
      <div
        v-if="field.options.presentationFormat != 'dropdownSelect' && field.options.presentationFormat != 'dropdownTable'"
        class="select-object-icon"
        :class="[field.options.btnClass, field.options.disabled ? 'select-object-icon-disabled' : '']"
        ref="selectIcon"
        :disabled="field.options.disabled"
        @click="openDialog"
      >
        <em class="iconfont" :class="[field.options.btnIcon ? field.options.btnIcon : 'icon-chaxun']"></em>
      </div>
      <div v-if="field.options.isAbleRefresh" ref="refreshIcon" class="select-object-icon" :disabled="field.options.disabled" @click="refreshSelectedObject">
        <em class="iconfont icon-shuaxin"></em>
      </div>
      <SelectObjectSelect
        :virtualRef="$refs.widgetWrapRef"
        :width="popoverWidth"
        v-if="field.options.presentationFormat == 'dropdownSelect'"
        ref="selectObjSelectRef"
        :field="field"
        :fieldModel="fieldModel"
        :firstNotLoadData='firstNotLoadData'
        @confirm="confirmSelect"
      />
      <SelectObjectTable
        :virtualRef="$refs.widgetWrapRef"
        :width="popoverWidth"
        v-else-if="field.options.presentationFormat == 'dropdownTable'"
        ref="selectObjTableRef"
        :field="field"
        :fieldModel="fieldModel"
        :firstNotLoadData='firstNotLoadData'
        @confirm="confirmTable"
      />
      <!-- <select-object-dialog
        v-else
        v-model="showDialog"
        :field="field"
        :parent-widget="parentWidget"
        :customUrlParams="customUrlParams"
        :firstNotLoadData='firstNotLoadData' 
        @confirm="confirmFunc"
      /> -->
        <treeTalbe
        v-else 
          ref="selectObjectRef"
        v-model="showDialog"
        :field="field"
        :parent-widget="parentWidget"
        :customUrlParams="customUrlParams"
        :firstNotLoadData='firstNotLoadData' 
        @confirm="confirmFunc"
      />
       
  
    </div>
  </template>
  </form-item-wrapper>
</template>

<script>

import formDesigner from '@common/form-designer';
import core from '@common/core'
const request = core.getExportResource('src/core/utils/http-tools.js').default;
const selectObj= formDesigner.getExportResource('src/form-designer/extension/form-designer/rewrite-field-widget/select-object.vue')
  import treeTalbe from './tree-talbe.vue';
  console.log('treeTalbe',treeTalbe)
 export default {
  name: 'select-object-widget',
  componentName: 'FieldWidget', 
  extends: selectObj.default,
     components: {treeTalbe},
  data() {
    return {
      tableData: [],   // 树形数据
      columns: []      // 动态列配置
    };
  },
  
 mounted() {
  console.log(222222222)
   console.log(this.field)
  console.log(this.parentWidget)
   console.log(this.firstNotLoadData)
    console.log(this.customUrlParams)
        console.log(this.confirmFunc)
          console.log(222222222)
 },
 methods:{
   // 父组件方法：获取子组件中勾选的行
  async getSelectedFromChild() {
    const childComp = this.$refs.selectObjectRef
    
    if (!childComp) {
      console.warn('⚠️ 子组件未渲染或 ref 未绑定')
      return
    }

    // ✅ 调用子组件暴露的方法
    const selectedRows = childComp.getSelectedRows?.()
    console.log('🎯 父组件拿到选中行:', selectedRows)
    return selectedRows
  },

  // 父组件方法：主动触发子组件的 confirm
  triggerChildConfirm() {
    const childComp = this.$refs.selectObjectRef
    if (childComp && typeof childComp.triggerConfirm === 'function') {
      childComp.triggerConfirm() // 触发子组件的 confirmFunc
    }
  },
      confirmFunc(data) {
  
         console.log("confirmFunc",this.getSelectedFromChild())
        console.log(data)
      if (this.field.options.isMultiple) {
        if (this.field.options.presentationFormat == 'dialog') {
          // modify by nieyz 2025/1/22 选中的数据做追加不直接替换原来的数据
          data.forEach((each) => {
            const index = this.fieldModel.findIndex((e) => e.oid === each.oid);
            index > -1 ? this.fieldModel.splice(index, 1, each) : this.fieldModel.push(each);
          });
        } else if (this.field.options.presentationFormat == 'dropdownSelect' || this.field.options.presentationFormat == 'dropdownTable') {
          if (data.selected) {
            const attr = this.field.options.objectSelectData?.attrMap[0]?.attr;
            data.widgetShowText = this.transSpanToValue(attr, data);
            this.fieldModel.push(data);
          } else {
            this.fieldModel = this.fieldModel.filter((val) => val.oid != data.oid);
          }
        }
        this.handleChangeEvent(this.fieldModel);
      } else {
        this.transRowToModel(data);
      }
    },
    // 下拉选择的确认
    confirmSelect(data) {
      if (!this.field.options.isMultiple) {
        this.$refs.selectObjSelectRef.setVisible(false);
      }
      this.confirmFunc(data);
    },
    // 下拉表格的确认
    confirmTable(data) {
      if (!this.field.options.isMultiple) {
        this.$refs.selectObjTableRef.setVisible(false);
      }
      this.confirmFunc(data);
    }

  },

  
};
</script>
